# tests for util.py

import unittest
from brightcontent import util
from brightcontent.core import store

sample_entry_xml = 'sample_xml/entry.xml'

class TestUtilFunctions(unittest.TestCase):

    def setUp(self):
        self.sample_xml = sample_entry_xml
    
    def test_fixup_namespaces(self):
        pass

    def test_quick_xml_scan(self):
        published = util.quick_xml_scan(self.sample_xml,
                                        'atom:published',
                                        prefixes=store.COMMON_PREFIXES)
        self.assertEqual(published, '2006-10-2T11:10:35Z')

    def test_get_base_url(self):
        pass

class TestUrlDispatch(unittest.TestCase):

    def setUp(self):
        self.urls = (
            (r'/(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/(?P<slug>[a-z\-\;\)\:]+)(?P<atom>/index.atom)?', 'slug'),
            (r'/(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})(?P<atom>/index.atom)?', 'day'),
            (r'/(?P<year>\d{4})/(?P<month>\d{1,2})(?P<atom>/index.atom)?', 'month'),
            (r'/(?P<year>\d{4})(?P<atom>/index.atom)?', 'year'),
            (r'/(?P<atom>index.atom)?', 'index')
        )
        self.test_urls = {
            'year' : ('/2006', '/2006/'),
            'month' : ('/2005/5', '/2005/10', '/2005/5/', '/2005/10/'),
            'day' : ('/2005/5/15', '/2005/10/15', '/2005/5/5', '/2005/10/5',
                     '/2005/5/15/', '/2005/10/15/', '/2005/5/5/', '/2005/10/5/'),
            'slug' : ('/2005/5/15/some-slug-title', '/2005/10/15/some-slug-title',
                      '/2005/5/5/some-slug-title/', '/2005/10/5/some-slug-title/')
        }

    def test_match(self):
        m = util.UrlDispatch(self.urls)
        for mtype, url_list in self.test_urls.items():
            for url in url_list:
                func, query_dict = m.match(url)
                self.assertEqual(func, mtype)

if __name__ == '__main__':
    unittest.main()
